package com.ruoyi.web.util;

import com.alibaba.fastjson.*;
import org.dom4j.*;

import java.util.List;


public class Xml2Json 
{
	/**
	 * 将xml转换为JSON对象
	 * @param xml xml字符串
	 * @return
	 * @throws Exception
	 */ 
	public static JSONObject xmltoJson(String xml) throws Exception {
		 JSONObject jsonObject = new JSONObject();
		 Document document = DocumentHelper.parseText(xml); //获取根节点元素对象  
		 Element root = document.getRootElement(); 
		 iterateNodes(root, jsonObject); 
		 return jsonObject; 
	 }
	
	/**
	 * 遍历元素
	 * @param node 元素
	 * @param json 将元素遍历完成之后放的JSON对象
	 */ 
	 public static void iterateNodes(Element node,JSONObject json)
	 { 
		 //获取当前元素的名称 
		 String nodeName = node.getName(); //判断已遍历的JSON中是否已经有了该元素的名称 
		 if(!json.isEmpty() && json.get(nodeName)!=null)
		 { 
			 //该元素在同级下有多个 
			 Object Object = json.get(nodeName); 
			 JSONArray array = null; 
			 if(Object instanceof JSONArray)
			 {
				 array = (JSONArray) Object; 
			 }
			 else 
			 { 
				 array = new JSONArray(); 
				 array.add(Object);
			 } 
			 //获取该元素下所有子元素
			 List<Element> listElement = node.elements(); 
			 if(listElement.isEmpty())
			 { 
				 //该元素无子元素，获取元素的值 
				 String nodeValue = node.getTextTrim(); 
				 array.add(nodeValue);
				 json.put(nodeName, array);
				 return ; 
			  } 
			 //有子元素
			 JSONObject newJson = new JSONObject(); 
			 //遍历所有子元素 
			 for(Element e:listElement)
			 { 
				 //递归 
				 iterateNodes(e,newJson); 
			 } 
			 array.add(newJson);
			 json.put(nodeName, array); 
			 return ; 
		} 
		 //该元素同级下第一次遍历
         //获取该元素下所有子元素
		 List<Element> listElement = node.elements();
		 if(listElement.isEmpty())
		 { 
			 //该元素无子元素，获取元素的值
			 String nodeValue = node.getTextTrim();
			 json.put(nodeName, nodeValue);
			 return ;
		 } //有子节点，新建一个JSONObject来存储该节点下子节点的值 
		 JSONObject object = new JSONObject(); //遍历所有一级子节点  
		 for(Element e:listElement)
		 { 
			 //递归  
			 iterateNodes(e,object);
			 
		 }
         json.put(nodeName, object);
        return ; 
    }  
	 
	 public static void main(String[] args) throws Exception {
//		 String xml = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
//				 "   <soap:Body>\n" +
//				 "      <QeuryGJAIDByIdNoResponse xmlns=\"http://microsoft.com/webservices/\">\n" +
//				 "         <QeuryGJAIDByIdNoResult>{\"SUCCESS\":\"1\",\"MESSAGE\":\"\",\"DATA\":[]}</QeuryGJAIDByIdNoResult>\n" +
//				 "      </QeuryGJAIDByIdNoResponse>\n" +
//				 "   </soap:Body>\n" +
//				 "</soap:Envelope>";

		 String xml1 = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
				 "   <soap:Body>\n" +
				 "      <PopulationIndexResponse xmlns=\"http://microsoft.com/webservices/\">\n" +
				 "         <PopulationIndexResult><![CDATA[<?xml version=\"1.0\" encoding=\"GB2312\"?><response><index><person><patient_id>059827044</patient_id><health_record_code></health_record_code><health_card_id></health_card_id><name>刘纪凤</name><sex>女</sex><marital_status>初婚</marital_status><date_of_birth>1983/10/19</date_of_birth><birth_place1>广西壮族自治区南宁市西乡塘区</birth_place1><birth_place2>广西壮族自治区南宁市西乡塘区安吉街道办事处秀安路社区居委会</birth_place2><citizenship>中国</citizenship><nation>壮族</nation><id_no>452130198310190045</id_no><certificate_type>身份证</certificate_type><identity></identity><charge_type></charge_type><unit_in_contract></unit_in_contract><mailing_address1>广西壮族自治区南宁市西乡塘区</mailing_address1><mailing_address2>广西壮族自治区南宁市西乡塘区安吉街道办事处秀安路社区居委会</mailing_address2><mailing_address3>广西壮族自治区南宁市西乡塘区</mailing_address3><mailing_address4>广西壮族自治区南宁市西乡塘区安吉街道办事处秀安路社区居委会</mailing_address4><zip_code></zip_code><phone_number_home></phone_number_home><phone_number_business></phone_number_business><next_of_kin></next_of_kin><relationship></relationship><next_of_kin_addr></next_of_kin_addr><next_of_kin_zip_code></next_of_kin_zip_code><next_of_kin_phone></next_of_kin_phone><ispoor>0</ispoor><dead_tag>0</dead_tag><tprq></tprq><out_poverty_status></out_poverty_status><attached_address></attached_address><attached_hujiaddress></attached_hujiaddress><lasttime></lasttime><lastuser></lastuser><lastunit></lastunit></person><person><patient_id>080550625</patient_id><health_record_code></health_record_code><health_card_id></health_card_id><name>玉丽芬</name><sex>女</sex><marital_status>初婚</marital_status><date_of_birth>1983/10/19</date_of_birth><birth_place1>广西壮族自治区崇左市大新县</birth_place1><birth_place2>广西壮族自治区崇左市大新县农场华侨农场场部</birth_place2><citizenship>中国</citizenship><nation>汉族</nation><id_no>452130198310190045</id_no><certificate_type></certificate_type><identity></identity><charge_type></charge_type><unit_in_contract></unit_in_contract><mailing_address1>广西壮族自治区崇左市大新县</mailing_address1><mailing_address2>广西壮族自治区崇左市大新县农场华侨农场场部</mailing_address2><mailing_address3>广西壮族自治区崇左市大新县</mailing_address3><mailing_address4>广西壮族自治区崇左市大新县农场华侨农场场部</mailing_address4><zip_code></zip_code><phone_number_home></phone_number_home><phone_number_business></phone_number_business><next_of_kin></next_of_kin><relationship></relationship><next_of_kin_addr></next_of_kin_addr><next_of_kin_zip_code></next_of_kin_zip_code><next_of_kin_phone></next_of_kin_phone><ispoor>0</ispoor><dead_tag>0</dead_tag><tprq></tprq><out_poverty_status></out_poverty_status><attached_address></attached_address><attached_hujiaddress></attached_hujiaddress><lasttime></lasttime><lastuser></lastuser><lastunit></lastunit></person></index></response>]]></PopulationIndexResult>\n" +
				 "      </PopulationIndexResponse>\n" +
				 "   </soap:Body>\n" +
				 "</soap:Envelope>";
		 String xml2 ="<response><index><person><patient_id>059827044</patient_id><health_record_code></health_record_code><health_card_id></health_card_id><name>刘纪凤</name><sex>女</sex><marital_status>初婚</marital_status><date_of_birth>1983/10/19</date_of_birth><birth_place1>广西壮族自治区南宁市西乡塘区</birth_place1><birth_place2>广西壮族自治区南宁市西乡塘区安吉街道办事处秀安路社区居委会</birth_place2><citizenship>中国</citizenship><nation>壮族</nation><id_no>452130198310190045</id_no><certificate_type>身份证</certificate_type><identity></identity><charge_type></charge_type><unit_in_contract></unit_in_contract><mailing_address1>广西壮族自治区南宁市西乡塘区</mailing_address1><mailing_address2>广西壮族自治区南宁市西乡塘区安吉街道办事处秀安路社区居委会</mailing_address2><mailing_address3>广西壮族自治区南宁市西乡塘区</mailing_address3><mailing_address4>广西壮族自治区南宁市西乡塘区安吉街道办事处秀安路社区居委会</mailing_address4><zip_code></zip_code><phone_number_home></phone_number_home><phone_number_business></phone_number_business><next_of_kin></next_of_kin><relationship></relationship><next_of_kin_addr></next_of_kin_addr><next_of_kin_zip_code></next_of_kin_zip_code><next_of_kin_phone></next_of_kin_phone><ispoor>0</ispoor><dead_tag>0</dead_tag><tprq></tprq><out_poverty_status></out_poverty_status><attached_address></attached_address><attached_hujiaddress></attached_hujiaddress><lasttime></lasttime><lastuser></lastuser><lastunit></lastunit></person><person><patient_id>080550625</patient_id><health_record_code></health_record_code><health_card_id></health_card_id><name>玉丽芬</name><sex>女</sex><marital_status>初婚</marital_status><date_of_birth>1983/10/19</date_of_birth><birth_place1>广西壮族自治区崇左市大新县</birth_place1><birth_place2>广西壮族自治区崇左市大新县农场华侨农场场部</birth_place2><citizenship>中国</citizenship><nation>汉族</nation><id_no>452130198310190045</id_no><certificate_type></certificate_type><identity></identity><charge_type></charge_type><unit_in_contract></unit_in_contract><mailing_address1>广西壮族自治区崇左市大新县</mailing_address1><mailing_address2>广西壮族自治区崇左市大新县农场华侨农场场部</mailing_address2><mailing_address3>广西壮族自治区崇左市大新县</mailing_address3><mailing_address4>广西壮族自治区崇左市大新县农场华侨农场场部</mailing_address4><zip_code></zip_code><phone_number_home></phone_number_home><phone_number_business></phone_number_business><next_of_kin></next_of_kin><relationship></relationship><next_of_kin_addr></next_of_kin_addr><next_of_kin_zip_code></next_of_kin_zip_code><next_of_kin_phone></next_of_kin_phone><ispoor>0</ispoor><dead_tag>0</dead_tag><tprq></tprq><out_poverty_status></out_poverty_status><attached_address></attached_address><attached_hujiaddress></attached_hujiaddress><lasttime></lasttime><lastuser></lastuser><lastunit></lastunit></person></index></response>";

		 JSONObject jsonObject = xmltoJson(xml2);
		 System.out.println(jsonObject);
	 
	 }
}
